Everything about Switch Statement totally explained
In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (for example, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that's syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.
In most languages, a switch statement is defined across many individual statements. A typical syntax is that the first line contains the actual word "switch" followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take.
Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats.
An optional special block is also allowed, which doesn't specify any value and which begins with the default keyword instead of the case keyword. If this block is present and if none of the values listed for any other block matches that of the control variable, program execution will jump to the statement following the default keyword.
The method of terminating a block is also of note. Typically, a break keyword is used to signal the end of the block. When encountered, this keyword causes program execution to continue with the first statement after the series of statements within the switch statement, thus completing execution of the switch statement. If no break keyword is present at the end of the block, in many languages program execution "falls through" to the code associated with the next block in the switch statement, as if its value also matched the value of the control variable. Notable exceptions include C#, in which fallthrough isn't permitted unless the block is empty and all blocks must be terminated via a break, or by using another keyword. Similarly, almost all BASIC dialects that feature this type of statement don't allow fallthrough.
Examples
The following are simple examples, written in the various languages, that use switch statements to print one of several possible lines, depending on the value of an integer entered by the user. The lack of break keywords to cause fall through of program execution from one block to the next is used extensively. For example, if n=5, the second case statement will produce a match to the control variable. Since there are no statements following this line and no break keyword, execution continues through the 'case 7:' line and to the next line, which produces output. The break line after this causes the switch statement to conclude. If the user types in more than one digit, the default block is executed, producing an error message.
C
switch(n)
In Ruby, due to its handling of === equality, the statement can be used to test for variable’s class:
case input
when Array: puts 'input is an Array!'
when Hash: puts 'input is a Hash!'
end
Ruby also returns a value that can be assigned to a variable, and doesn’t actually require the case to have any parameters (acting a bit like an else if statement):
catfood = case
when cat.age <= 1: junior
when cat.age > 10: senior
else normal
end
Compilation
If the constants form a compact range then a switch statement can be implemented very efficiently as if it were a choice based on whole numbers. This is often done by using a jump table.
Advantages and disadvantages
In some languages and programming environments, a case or switch statement is considered easier to read and maintain than an equivalent series of if-else statements, because it's more concise. However, when implemented with fall-through, switch statements are a frequent source of bugs among programmers new to the switch statement.
Alternatives
One alternative to a switch statement can be the use of a lookup table which contains as keys the case values and as values the part under the case statement. In some languages, only actual data types are allowed as values in the lookup table. In other languages, it's also possible to assign functions as lookup table values, gaining the same flexibility as a real switch statement (this is one way to implement switch statements in Lua which has no built-in switch ).
In some cases, lookup tables are more efficient than switch statements as many languages can optimize the table lookup whereas switch statements are often not optimized that much.
Another "alternative" to switch statements is the extensive use of polymorphism.
Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:
Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.